package com.dlc.felear.xiaoensale.utils;

import android.util.Log;

import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.dlc_serial_port.DlcSerialPortClient;
import com.dlc.felear.xiaoensale.entity.HotInfo;
import com.dlc.felear.xiaoensale.factory.CmdList;
import com.dlc.felear.xiaoensale.helper.SerialManager;

import org.simple.eventbus.EventBus;

import java.util.Arrays;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class HotModelUtil {
    private static final String TAG="HotModelUtil";
    private static HotModelUtil instance;
    private ScheduledExecutorService scheduledThreadPool;
    private HotModelUtilCallback hotModelUtilCallback;
    private static final int POWERSIZE=31;
    private boolean enable=true;


    public HotModelUtil setHotModelUtilCallback(HotModelUtilCallback hotModelUtilCallback) {
        this.hotModelUtilCallback = hotModelUtilCallback;
        return instance;
    }

    public static HotModelUtil getInstance() {
        if(instance==null){
            synchronized (HotModelUtil.class){
                if(instance==null){
                    instance=new HotModelUtil();
                }
            }
        }
        return instance;
    }

    public void setEnable(boolean enable){
        this.enable=enable;
        Log.e(TAG, "enable:" +enable);
    }
    private HotModelUtil() {
        scheduledThreadPool = new ScheduledThreadPoolExecutor(1);
//        startTimer();
        Log.e(TAG, "HotModelUtil:" );
    }

//    private void startTimer() {
//        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    loopRun();
//                } catch (Exception e) {
//
//                }
//            }
//        }, 1000, 1000, TimeUnit.MILLISECONDS);
//    }
//
//    private void loopRun() {
//        if(enable){
////            sendCmd(CmdList.DcHotStatus());
//        }
//    }

//    public void recieveData1(byte[] data){
//        Log.e(TAG, "接收数据:" + HexUtil.bytes2HexString(data));
//        if(CmdList.checkSum16(data)&&data[0]==(byte) 0xab&&data.length>5){
//            int power = ((data[4] & 0xff) << 8) | (data[5] & 0xff);
//            int temp = ((data[6] & 0xff) << 8) | (data[7] & 0xff);
//            byte error = data[8];
//            Log.e(TAG,"功率="+power+" 温度="+temp+" 错误码="+error);
//            if(hotModelUtilCallback!=null){
//                hotModelUtilCallback.hotModelUtilCallback(power/POWERSIZE,temp,error);
//            }
//            EventBus.getDefault().post(new HotInfo(power/POWERSIZE,temp,getErrorString(error)),
//                    Constant.ACTION_HOT_INFO_RESULT);
//        }else {
//            Log.e(TAG, "检验失败:" + HexUtil.bytes2HexString(data));
//        }
//    }

    public void handleHot(int level){
        if(level>=8){
            level=8;
        }
        if(level<0){
            level=0;
        }
//        sendCmd(CmdList.setDcHot(level,150));
    }

    private void sendCmd(byte[] setDcHot) {
        if(enable){
            DlcSerialPortClient.getInstance().send(Constant.TTYS0, setDcHot);
        }
    }


    public interface HotModelUtilCallback{
        void hotModelUtilCallback(int power,int temp,byte error);
    }

    public String getErrorString(int error){
        String temp="待机";
        switch (error){
            case 0:
                temp="正常";
                break;
            case 1:
                temp="无锅";
                break;
            case 2:
                temp="线盘超温";
                break;
            case 3:
                temp="线盘温度探头开路";
                break;
            case 4:
                temp="IGBT超温";
                break;
            case 5:
                temp="IGBT温度探头开路";
                break;
            case 6:
                temp="锅超温";
                break;
            case 7:
                temp="锅温度探头开路";
                break;
            case 8:
                temp="输入电压过低(缺相)";
                break;
            case 9:
                temp="输入电压过高";
                break;
            case 10:
                temp="输出电流故障";
                break;
            case 11:
                temp="IGBT故障";
                break;
            case 12:
                temp="负载故障(错锅)";
                break;
            case 13:
                temp="火力开关故障";
                break;
            case 14:
                temp="火力开关故障";
                break;
            case 15:
                temp="环境温度过高";
                break;
            case 16:
                temp="硬件故障";
                break;
        }
        return temp;
    }

}
